/*
  Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace XnaDevRu.BulletX
{
	[Serializable]
	public enum ActivationState
	{
		Nothing = 0,
		Active,
		IslandSleeping,
		WantsDeactivation,
		DisableDeactivation,
		DisableSimulation,
	}

	[Serializable]
	public enum CollisionOptions
	{
		StaticObject = 1,
		KinematicObject = 2,
		NoContactResponse = 4,
		CustomMaterialCallback = 8,//this allows per-triangle material (friction/restitution)
	}

	/// <summary>
	/// btCollisionObject can be used to manage collision detection objects. 
	/// btCollisionObject maintains all information that is needed for a collision detection: Shape, Transform and AABB proxy.
	/// They can be added to the btCollisionWorld.
	/// </summary>
	[Serializable]
	public class CollisionObject
	{
		public Matrix WorldTransform;
		public BroadphaseProxy Broadphase;
		public CollisionShape CollisionShape;

		//m_interpolationWorldTransform is used for CCD and interpolation
		//it can be either previous or future (predicted) transform
		public Matrix InterpolationWorldTransform;

		public CollisionOptions CollisionFlags;

		public int IslandTag;
		public ActivationState ActivationState = ActivationState.Active;
		public float DeactivationTime;

		public float Friction;
		public float Restitution;

		//users can point to their objects, m_userPointer is not used by Bullet
		public object UserData;

		//m_internalOwner one is used by optional Bullet high level interface
		public object Owner;

		//time of impact calculation
		public float HitFraction = 1;

		//Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
		public float CcdSweptSphereRadius;

		// Don't do continuous collision detection if square motion (in one step) is less then m_ccdSquareMotionTreshold
		public float CcdSquareMotionThreshold;
		//those two are experimental: just added for bullet time effect, so you can still apply impulses (directly modifying velocities) 
		//without destroying the continuous interpolated motion (which uses this interpolation velocities)
		public Vector3 InterpolationLinearVelocity;
		public Vector3 InterpolationAngularVelocity;
		// If some object should have elaborate collision filtering by sub-classes
		protected bool CheckCollide;

		public int CompanionID;

		public bool IsStaticObject { get { return ( CollisionFlags & CollisionOptions.StaticObject ) != 0; } }
		public bool IsKinematicObject { get { return ( CollisionFlags & CollisionOptions.KinematicObject ) != 0; } }
		public bool IsStaticOrKinematicObject { get { return ( CollisionFlags & ( CollisionOptions.KinematicObject | CollisionOptions.StaticObject ) ) != 0; } }

		public bool HasContactResponse { get { return ( CollisionFlags & CollisionOptions.NoContactResponse ) == 0; } }
		public bool MergesSimulationIslands
		{
			get
			{
				//static objects, kinematic and object without contact response don't merge islands
				return ( CollisionFlags & ( CollisionOptions.StaticObject | CollisionOptions.KinematicObject | CollisionOptions.NoContactResponse ) ) == 0;
			}
		}

		public void SetActivationState( ActivationState value )
		{
			if( ( ActivationState != ActivationState.DisableDeactivation ) && ( ActivationState != ActivationState.DisableSimulation ) )
				ActivationState = value;
		}

		public bool IsActive { get { return ( ( ActivationState != ActivationState.IslandSleeping ) && ( ActivationState != ActivationState.DisableSimulation ) ); } }

		public void ForceActivationState( ActivationState newState )
		{
			ActivationState = newState;
		}

		public void Activate()
		{
			Activate( false );
		}

		public void Activate( bool forceActivation )
		{
			if( forceActivation || ( CollisionFlags & ( CollisionOptions.StaticObject | CollisionOptions.KinematicObject ) ) == 0 )
			{
				ActivationState = ActivationState.Active;
				DeactivationTime = 0;
			}
		}

		public bool CheckCollideWith( CollisionObject co )
		{
			if( CheckCollide )
				return CheckCollideWithOverride( co );

			return true;
		}


		protected virtual bool CheckCollideWithOverride( CollisionObject co )
		{
			return true;
		}
	}
}
